﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SmartProject.BusinessObjects;
using System.Collections.Generic;

namespace SmartProject.AppSpace
{
    public partial class Dashboard : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.LblCurrentDate.Text = DateTime.Today.ToLongDateString();
                if (UI.BasePage.IsFirstTimePreferenceSetup || 
                    UI.BasePage.IsFirstTimeUserSetup)
                {
                    this.PnlUserFunctions.Visible = false;
                    this.PnlNavigation.Visible = false;
                    this.BtnChangeMyPassword.Visible = false;
                }
                else
                {
                    UI.BasePage basePage = (UI.BasePage)this.Page;
                    if (basePage.IsFirstTimeManagementSetup)
                    {
                        this.PnlNavigation.Visible = false;
                        this.BtnChangeMyPassword.Visible = false;
                    }
                    else
                    {
                        this.PnlNavigation.Visible = true;
                        this.BtnChangeMyPassword.Visible = true;
                    }
                    PnlUserFunctions.Visible = true;
                    this.BtnSignOut.Text = String.Format(this.BtnSignOut.Text, basePage.FullName);
                    List<string> roles = new List<string>(Roles.GetRolesForUser());
                    if (roles.Count > 0)
                    {
                        if (roles.Contains("Administrator"))
                        {
                            this.MnuAdminOptions.Visible = true;
                        }
                        else
                        {
                            this.MnuAdminOptions.Visible = false;
                        }

                        // Show first role as an example.
                        this.LblMenu.Text = roles[0];
                    }
                }
            }
        }

        protected void BtnSignOut_Click(object sender, EventArgs e)
        {
            // You cannot directly delete a cookie on a user's computer. 
            // However, you can direct the user's browser to delete the 
            // cookie by setting the cookie's expiration date to a past date.
            HttpCookie expireCookie = new HttpCookie("FullName");
            expireCookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(expireCookie);

            UI.BasePage basePage = (UI.BasePage)this.Page;
            basePage.Signout();
            FormsAuthentication.SignOut();
            Response.Redirect("~/Default.aspx");
        }

        protected void BtnNewWorkItem_Click(object sender, EventArgs e)
        {
            Cache["WorkID"] = String.Empty;
            Server.Transfer("~/AppSpace/Work.aspx");
        }

        protected void BtnMyPage_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AppSpace/MyPage.aspx");
        }

        protected void BtnSetup_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AppSpace/Setup.aspx");
        }

        protected void BtnManageUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AppSpace/Users.aspx");
        }

        protected void BtnChangeMyPassword_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AppSpace/ChangePassword.aspx");
        }

        protected void BtnProductBacklog_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AppSpace/ProductBacklog.aspx");
        }

        protected void BtnManagement_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AppSpace/Management.aspx");
        }

        protected void BtnSprintCycle_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AppSpace/Sprints.aspx");
        }
    }
}
